package com.china08.yxyapi.api.schwebsite.comm;

import java.text.ParseException;
import java.time.LocalDate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSettingDomains;
import com.china08.yxyapi.entity.mg.sws.SwsPvForDailyAnalyze;
import com.china08.yxyapi.entity.mg.sws.SwsPvForTotalAnalyze;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSettingDomainsRepository;
import com.china08.yxyapi.repository.mg.SwsPvForDailyAnalyzeRepository;
import com.china08.yxyapi.repository.mg.SwsPvForTotalAnalizeRepository;
import com.china08.yxyapi.util.RabbitMqSendUtil;

@CrossOrigin
@RestController("SwsCommPvController")
public class SwsCommPvController extends YxyApiBaseController {

    @Autowired
    private SwsPvForDailyAnalyzeRepository swsPvForDailyAnalyzeRepository;

    @Autowired
    private SwsSettingDomainsRepository swsSettingDomainsRepository;

    @Autowired
    private SwsPvForTotalAnalizeRepository swsPvForTotalAnalizeRepository;

    @RequestMapping(value = "/api_yw/schwebsite/comm/pv", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) throws ParseException {

        String domainName = getDomainName(request);
        Assert.isTrue(StringUtils.isNotBlank(domainName), "域名为空");
        // 通过域名获取学校ID
        SwsSettingDomains byDomainName = swsSettingDomainsRepository.findByDomainName(domainName);
        Assert.isTrue(null != byDomainName, "域名对象为空");
        // 当天的日期处理
        String schoolId = byDomainName.getSchoolId();

        // 修改学校访问量
        RabbitMqSendUtil.schoolWebsitPvUpd(schoolId);

        LocalDate curDate = LocalDate.now();
        Integer yearForPv = curDate.getYear();
        Integer monthForPv = curDate.getMonthValue();
        Integer dayForPv = curDate.getDayOfMonth();
        SwsPvForDailyAnalyze pvForDailyAnalyze = swsPvForDailyAnalyzeRepository
                .findBySchoolIdAndYearForPvAndMonthForPvAndDayForPv(schoolId, yearForPv, monthForPv, dayForPv);
        RespModel respModel = new RespModel();
        if (null == pvForDailyAnalyze) {
            respModel.dailyPv = 1L;
        } else {
            respModel.dailyPv = pvForDailyAnalyze.getPv();
        }
        respModel.schoolId = schoolId;
        SwsPvForTotalAnalyze totalPv = swsPvForTotalAnalizeRepository.findBySchoolId(schoolId);

        if (null == totalPv) {
            respModel.totalPv = 1L;
        } else {
            respModel.totalPv = totalPv.getPv();
        }
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

    }

    protected static class RespModel extends YxyApiRespModel {

        public Long totalPv;

        public Long dailyPv;

        public String schoolId;
    }
}
